/**
 * Created by Junchuan.Wu on 2015/12/5 0005.
 */
var db,//数据库实例
    username,//用户名
    password,//密码
    favorite;//喜好
var login_username,//登录用户名
    login_password;//登录密码
var input_username;//搜索的用户名
var show_username,//显示的用户名
    show_password,//显示的密码
    show_favorite;//显示的喜好
/**
 * 已经抽取到initialization.js
 */
function init(){
    username = document.getElementById('username');
    password = document.getElementById('password');
    favorite = document.getElementById('favorite');
    login_username = document.getElementById('login_username');
    login_password = document.getElementById('login_password');

    show_username = document.getElementById('show_username');
    show_password = document.getElementById('show_password');
    show_favorite = document.getElementById('show_favorite');
    input_username = document.getElementById('input_username');

    if(window.openDatabase===undefined) {
        alert("您的浏览器不支持本地存您的浏览器不支持webSQL Database API。");
    }else{
        db = openDatabase('mydb','1.1','mydb',1000);
        db.transaction(function (tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS my_users(username,password,favorite)');
        });
    }
}
/**
 * 插入数据
 * 判断username是否重复
 * 判断username&password是否为空
 */
function insert() {
    if(window.openDatabase===undefined) {
        alert("您的浏览器不支持本地存您的浏览器不支持webSQL Database API。");
    }else{
        db.transaction(function (tx) {
        //如果支持的话，就查询有没有相同的username
            tx.executeSql('SELECT * from my_users WHERE username=?',[username.value], function (tx,result) {
                //查询成功，判断结果集是否为空
                if(result.rows.length===0){
                    //如果结果集为空，执行插入操作
                    if(username.value!=""&&password.value!=""){
                        tx.executeSql('INSERT INTO my_users VALUES(?,?,?)',[username.value,password.value,favorite.value]);
                        alert("注册成功!"+"用户名为:"+username.value+"密码为:"+password.value);
                    }else{
                        alert("username和password不能为空!");
                    }
                }else{
                    alert('username重复!');
                }
            });
        });
    }
}
/**
 * 登录判断
 */
function login(){
    if(window.openDatabase===undefined) {
        alert("您的浏览器不支持本地存您的浏览器不支持webSQL Database API。");
    }else{
        db.transaction(function (tx) {
            tx.executeSql('SELECT * FROM my_users WHERE username=? AND password=?',[login_username.value,login_password.value], function (tx,result) {
                if(result.rows.length!=0){
                    alert('欢迎回来！'+result.rows[0]['username']+"\n您的喜爱的浏览器是:"+result.rows[0]['favorite']);
                }else{
                    alert('登陆失败,账号或密码有误');
                }
            });
        });
    }
}
/**
 * 查找用户，显示在前台上
 */
function show(){
    if(window.openDatabase===undefined) {
        alert("您的浏览器不支持本地存您的浏览器不支持webSQL Database API。");
    }else{
        db.transaction(function (tx) {
            tx.executeSql('SELECT * FROM my_users WHERE username=?',[input_username.value], function (tx,result) {
                if(result.rows.length!=0){
                    show_username.value = result.rows[0]['username'];
                    show_password.value = result.rows[0]['password'];
                    show_favorite.value = result.rows[0]['favorite'];
                }else{
                    alert("没有找到该用户");
                    window.location.reload();
                }
            });
        });
    }

}
/**
 * 编辑用户
 */
function edit() {
    if (window.openDatabase === undefined) {
        alert("您的浏览器不支持本地存您的浏览器不支持webSQL Database API。");
    }else{
        db.transaction(function (tx) {
           tx.executeSql('UPDATE my_users set password=?,favorite=? where username =?',[show_password.value,show_favorite.value,show_username.value], function () {
               console.log("编辑成功");
           });
        });
    }
}
/**
 * 删除用户
 */
function remove_user() {
    if (window.openDatabase === undefined) {
        alert("您的浏览器不支持本地存您的浏览器不支持webSQL Database API。");
    } else {
        db.transaction(function (tx) {
            tx.executeSql('DELETE FROM my_users WHERE username=?', [show_username.value], function () {
                console.log("删除成功");
            });
        });
    }
}